<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Images as ImagesModel;
use app\common\RabbitMQClient;
use think\facade\Db;
use think\facade\Queue;
use app\admin\jobs\MarketSMCopywritingPublish as MarketSMCopywritingJob;
use app\admin\model\MarketSmCopywriting as MarketSmCopywritingModel;
use app\admin\model\MarketSmCopywritingTasks as MarketSmCopywritingTasksModel;
use app\admin\model\MarketSocialMediaAccount as MarketSocialMediaAccountModel;


class MarketSMCopywriting extends BaseService
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * 发布社媒文案到社媒平台
     * @param $param
     * @return void
     */
    public function publish($param)
    {
        try {
            $rabbitMQService = new RabbitMQClient();
            $ids = $param['ids'];

            // 使用事务确保消息发送和数据库更新的一致性
            Db::startTrans();

            foreach ($ids as $id) {
                $rabbitMQService->sendMessage('newsPublish', json_encode(['id' => $id]));
            }

            Db::name('market_sm_copywriting')->whereIn('id', $ids)->update(['publish_status' => 2]);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => 1, 'message' => '操作失败: ' . $e->getMessage()];
        }

        $rabbitMQService->close();
        $this->logger->logOperation($ids,'MarketSMCopywriting','发布','发布社媒文案到社媒平台队列执行');
        return ['code' => 0, 'message' => '操作成功！'];
    }

    /**
     * 重新生成社媒文案
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function regenerate($param)
    {
        $oMarketSmCopywritingModel = MarketSmCopywritingModel::with(['crypto_news','market_news_search_det'])->find($param['id']);

        if (!$oMarketSmCopywritingModel || !$oMarketSmCopywritingModel->crypto_news || !$oMarketSmCopywritingModel->market_news_search_det) {
            return ['code' => 1, 'message' => '新闻或搜索结果不存在！'];
        }
        $oMarketSmCopywritingModel->task_status = 0;
        if (!$oMarketSmCopywritingModel->save()) {
            return ['code' => 1, 'message' => '操作失败！'];
        }
        // 压入队列
        $insertData = [
            'id' => $oMarketSmCopywritingModel->id
        ];
        // 压入队列
        $rabbitMQService = new RabbitMQClient();
        $rabbitMQService->sendMessage('createCopywriting', json_encode($insertData));
        $this->logger->logOperation($param['id'],'MarketSMCopywriting','重置','重置社媒文案');
        return ['code' => 0, 'message' => '操作成功！'];
    }

    /**
     * 修改社媒文案内容
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editContent($param)
    {
        $oMarketSmCopywritingModel = MarketSmCopywritingModel::find($param['id']);
        if (!$oMarketSmCopywritingModel) {
            return ['code' => 1, 'message' => '数据不存在！'];
        }
        $oMarketSmCopywritingModel->content = $param['content'];
        if (!$oMarketSmCopywritingModel->save()) {
            return ['code' => 1, 'message' => '操作失败！'];
        }
        $this->logger->logOperation($param['id'],'MarketSMCopywriting','修改','修改社媒文案内容');
        return ['code' => 0, 'message' => '操作成功！'];
    }
}
